// pages/contact/joinus.js
const app = getApp()
const nt_util = require('../../utils/nt_util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listitems: [],
    selectNum:0,
    typeId:'',
    name: '',
    storeName: '',
    phone: '',
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nt_util.that = this;
    this.getStore()
  },
  // 获取店铺类型列表
  getStore: function () {
    nt_util.httpRequest('api/life/v1/getStoreTypeList', 'post', {}, function (data) {
      nt_util.that.setData({
        listitems: data.data.list,
      })

    });
  },
  siwtchTap(e) {
    // const self = this;
    // this.setData({
    //    curIndex: e.currentTarget.dataset.index,
    //   // todo: e.currentTarget.dataset.name + '+',
    // })   
    
    let _selectNum = this.data.selectNum
    
    let _listitems = this.data.listitems
    let _selectStatus = _listitems[e.currentTarget.dataset.index].class
    var _typeId = this.data.listitems[e.currentTarget.dataset.index].id
    if (_selectStatus){
      _selectNum--
    }else{
      _selectNum++
    }
    if (_selectNum > 2) {
      return;
    }
    

    _listitems[e.currentTarget.dataset.index].class = !_selectStatus
    
    this.setData({
      listitems: _listitems,
      selectNum: _selectNum,
      typeId: (_selectNum==1) ? _typeId : this.data.typeId + ',' + _typeId
    }) 

  },
  checkParams: function () {
    var that = this;

    if (!that.data.typeId) {
      wx.showToast({
        title: '服务类型不能空',
      })
      return false;
    }
    if (!that.data.name) {
      wx.showToast({
        title: '商户姓名不能空',
      })
      return false;
    }
    if (!that.data.storeName) {
      wx.showToast({
        title: '商店名称不能空',
      })
      return false;
    }
    if (!that.data.phone) {
      wx.showToast({
        title: '号码不能为空',
      })
      return false;
    }
    return true;
  },
  bindKeyInput: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var data_type = e.detail.value;
    switch (dataset.type) {
      case 'name':
        that.setData({
          name: data_type
        });
        break;
      case 'storeName':
        that.setData({
          storeName: data_type
        });
        break;
      case 'phone':
        that.setData({
          phone: data_type
        });
        break;
    }
  },
  tapItem: function (event) {
    if (!this.checkParams()) {
      return false;
    }
    var queryObj = {
      typeId: this.data.typeId,
      name: this.data.name,
      storeName: this.data.storeName,
      phone: this.data.phone
    }
    //将对象转为string
    var queryBean = JSON.stringify(queryObj)
    var url = '/pages/contact/joinus2?queryBean=' +queryBean;
    wx.navigateTo({
      url: url
    })
  }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})